﻿/**
 * iframe - jQuery EasyUI
 * 
 * Licensed under the GPL:
 *   http://www.gnu.org/licenses/gpl.txt
 *
 * Copyright 2013 tavenli [ tavenli@qq.com ]
 * 基于Easy UI扩展实现的IFRAME弹出框
 *
 * Dependencies:
 * 	draggable
 * 	resizable
 * 	linkbutton
 * 	panel
 *  window
 */
(function($){

	$.iframe = {
		win: function(options) {
			var opts = $.extend({
				width: 300,
				height: 'auto',
				title: 'New IFrame Window',
				url:'',
				maximizable:false,
				minimizable:false,
				resizable:false,
				modal: true,
				buttons:'',
				style:'',
				parameter1_iframedialog:'',
				parameter2_iframedialog:'',
				parameter3_iframedialog:'',
                onClosed:function(){}
			}, options || {});

            $.iframe.data.onClosed = opts.onClosed;

			var iframe = $('<iframe id="iframe_dialog" name="iframe_dialog"/>').attr({
				width : '100%',
				height : '100%',
				frameborder : 0,
				scrolling:'yes',
				src : opts.url
			});

			var form = $('<form/>').attr({
				action:opts.url,
				method:'POST',
				target:'iframe_dialog'
			});
			var input1 = $('<input/>').attr({
				type:'hidden',
				name:'parameter1_iframedialog',
				value:opts.parameter1_iframedialog
			});
			var input2 = $('<input/>').attr({
				type:'hidden',
				name:'parameter2_iframedialog',
				value:opts.parameter2_iframedialog
			});
			var input3 = $('<input/>').attr({
				type:'hidden',
				name:'parameter3_iframedialog',
				value:opts.parameter3_iframedialog
			});
			form.append(input1);
			form.append(input2);
			form.append(input3);

			var win = $('<div/>').attr({style:opts.style}).appendTo('body');
			win.append(form);
			win.append(iframe);

			if(opts.style!='' && opts.style!=null){
				win.dialog({
					title: opts.title,
					modal: opts.modal,
					collapsible: false,
					minimizable: opts.minimizable,
					maximizable: opts.maximizable,
					resizable: opts.resizable,
					buttons: opts.buttons,
					onClose: function(){
						setTimeout(function(){
							win.window('destroy');
							//win.dialog('close');
							$.iframe.data.onClosed();
						}, 100);
					}
				});
			}
			else{
				win.dialog({
					title: opts.title,
					width: opts.width,
					height: opts.height,
					modal: opts.modal,
					collapsible: false,
					minimizable: opts.minimizable,
					maximizable: opts.maximizable,
					resizable: opts.resizable,
					buttons: opts.buttons,
					onClose: function(){
						setTimeout(function(){
							win.window('destroy');
							//win.window('close');
							$.iframe.data.onClosed();
						}, 100);
					}
				});
			}
			$.iframe.data.win = win;
			$.iframe.data.iframe = iframe;
			form.submit();
			form.remove();//这样From不会占用页面位置，不会出现多余的scroll

            return win;
		},
		onLoadSuccess:function(){
		},
		buttons:function(buttonsJsonArray){
			var _win = $.iframe.data.win;
			_win.dialog({
				buttons:buttonsJsonArray
			});
		},
        maximize:function(){
            var _win = $.iframe.data.win;
            _win.window("maximize");
            this.resizeFresh();
        },
        minimize:function(){
            $.iframe.data.win.window("minimize");
            this.resizeFresh();
        },
        restore:function(){
            $.iframe.data.win.window("restore");
        },
        resizeFresh:function(){
            var _win = $.iframe.data.win;
            var iframe = _win.find("iframe");
            iframe.css({width:_win.css("width"),height:_win.css("height")});
        },
		close:function(){
			var win = $.iframe.data.win;
			if(win){
				win.window('destroy');
				//win.window('close');
                $.iframe.data.onClosed();
			}
		}


	
	};
	
	$.iframe.data = {
		win:null,
        onClosed:function(){}
	};
	
})(jQuery);
